package com.sayyi.demo.test.common;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

/**
 * @author xuchuang
 * date 2021/12/23 3:44 PM
 */
@Data
@Schema(description = "通用返回数据，所有返回给前端的数据都是这个格式")
public class ResultBean<T> {

    public static <T> ResultBean<T> success(T data) {
        return new ResultBean<>(ResultStatus.SUCCESS, data);
    }
    public static <T> ResultBean<T> error(ResultStatus status) {
        return new ResultBean<>(status, null);
    }
    public static <T> ResultBean<T> error() {
        return new ResultBean<>(ResultStatus.ERROR, null);
    }
    public static <T> ResultBean<T> error(String msg) {
        return new ResultBean<>(ResultStatus.ERROR.getCode(), msg, null);
    }

    public ResultBean() {}
    public ResultBean(ResultStatus status, T data) {
        this(status.getCode(), status.getMsg(), data);
    }
    public ResultBean(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /** 自定义的状态码 */
    @Schema(description = "自定义的状态码，200为正常")
    private String code;
    /** 响应信息 */
    @Schema(description = "code不为200时，获取该字段内容提示用户")
    private String msg;
    /** 响应结果 */
    @Schema(description = "实际结果")
    private T data;
}
